<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>



<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.08">
<LINK rel="stylesheet" type="text/css" href="tutorial.css">
<TITLE>
Predicates, Goals and Queries
</TITLE>
</HEAD>
<BODY >
<A HREF="tutorial013.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="tutorial012.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="tutorial015.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2 CLASS="section"><A NAME="htoc27">3.2</A>&nbsp;&nbsp;Predicates, Goals and Queries</H2><UL>
<LI><A HREF="tutorial014.html#toc15">Conjunction and Disjunction</A>
</UL>

<A NAME="@default44"></A>
<A NAME="@default45"></A>
<A NAME="@default46"></A>
Where other programming languages have procedures and functions,
Prolog and ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> have <EM>predicates</EM>. A predicate is something
that has a truth value, so it is similar to a function with a boolean result.
A predicate <EM>definition</EM> simply defines what is true.
A predicate <EM>invocation</EM> (or <EM>call</EM>) checks whether something is true or false.
<A NAME="@default47"></A>
A simple example is the predicate <EM>integer/1</EM>, which has a built-in
definition. It can be called to check whether something is an integer:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
integer(123)           is true
integer(atom)          is false
integer([1,2])         is false
</PRE></BLOCKQUOTE>
A predicate call like the above is also called a <EM>goal</EM>.
A starting goal that the user of a program provides is called a <EM>query</EM>.
To show queries and their results, we will from now on
use the following notation:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
?- integer(123).
Yes.
?- integer(atom).
No.
?- integer([1,2]).
No.
</PRE></BLOCKQUOTE>
A query can simply be typed at the eclipse prompt, or entered into the
query field in a tkeclipse window. Note that it is not necessary to enter
the <TT>?-</TT> prefix.
On a console input, is however necessary to terminate the query with a
full-stop (a dot followed by a newline).
After executing the query, the system will print one of the
answers <B>Yes</B> or <B>No</B>.<BR>
<BR>
<A NAME="toc15"></A>
<H3 CLASS="subsection"><A NAME="htoc28">3.2.1</A>&nbsp;&nbsp;Conjunction and Disjunction</H3>
<A NAME="@default48"></A>
<A NAME="@default49"></A>
<A NAME="@default50"></A> <A NAME="@default51"></A>
Goals can be combined to form conjunctions (AND) or disjunctions (OR).
Because this is so common, Prolog uses the comma for AND and the
semicolon for OR. The following shows two examples of conjunction,
the first one is true because both conjuncts are true, the second is false:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
?- integer(5), integer(7).
Yes.
?- integer(5), integer(hello).
No.
</PRE></BLOCKQUOTE>
In contrast, a disjunction is only false if both disjuncts are false:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
?- ( integer(hello) ; integer(5) ).
Yes.
?- ( integer(hello) ; integer(world) ).
No.
</PRE></BLOCKQUOTE>
As in this example, it is advisable to always surround disjunctions with
parentheses. While not strictly necessary in this example, they are often
required to clarify the structure.<BR>
<BR>
In practice, when answering queries with disjunctions, the system will
actually give a separate <B>Yes</B> answer for every way in which the
query can be satisfied (i.e. proven to be true).
For example, the following disjunction can be satisfied in two ways,
therefore system will give two <B>Yes</B> answers:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
?- ( integer(5) ; integer(7) ).
Yes (0.00s cpu, solution 1, maybe more)
Yes (0.02s cpu, solution 2)
</PRE></BLOCKQUOTE>
The second answer will only be given after the user has explicitely
asked for more solutions.
Sometimes the system cannot decide whether an answer is the last one.
In that case, asking for more solutions may lead to an alternative
<B>No</B> answer, like in the following example:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
?- ( integer(5) ; integer(hello) ).
Yes (0.00s cpu, solution 1, maybe more)
No (0.02s cpu)
</PRE></BLOCKQUOTE>
Of course, as long as there was at least one <B>Yes</B> answer, the query
as a whole was true.<BR>
<BR>
<HR>
<A HREF="tutorial013.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="tutorial012.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="tutorial015.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
